import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _regeneratorRuntime from "@babel/runtime/regenerator";
// import { isEmpty } from 'lodash';
import { // setSession,
getResponse // getAccessToken,
} from "../utils/utils";
import { query as queryUsers, queryCurrent, queryTenant, // queryDefaultRole,
queryRoleList, updateCurrentRole as _updateCurrentRole, updateCurrentTenant as _updateCurrentTenant, updateDefaultTenant as _updateDefaultTenant, fetchDataHierarchiesList as _fetchDataHierarchiesList, switchDataHierarchies as _switchDataHierarchies, fetchDataHierarchiesSelectList as _fetchDataHierarchiesSelectList, queryDataHierarchies as _queryDataHierarchies } from "../services/user";
export default {
  namespace: 'user',
  state: {
    list: [],
    currentUser: {},
    tenantList: [],
    // 租户列表
    // defaultRole: {}, // 默认当前用户角色信息
    roleList: [],
    // 组织层查询分配给当前登录用户的角色列表
    hierarchicalList: [],
    hierarchicalSelectList: [],
    isModal: 0,
    isSelect: 0
  },
  effects: {
    fetch: /*#__PURE__*/_regeneratorRuntime.mark(function fetch(_, _ref) {
      var call, put, response;
      return _regeneratorRuntime.wrap(function fetch$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              call = _ref.call, put = _ref.put;
              _context.next = 3;
              return call(queryUsers);

            case 3:
              response = _context.sent;
              _context.next = 6;
              return put({
                type: 'save',
                payload: response
              });

            case 6:
            case "end":
              return _context.stop();
          }
        }
      }, fetch);
    }),
    fetchCurrent: /*#__PURE__*/_regeneratorRuntime.mark(function fetchCurrent(_, _ref2) {
      var call, put, user;
      return _regeneratorRuntime.wrap(function fetchCurrent$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              call = _ref2.call, put = _ref2.put;
              _context2.next = 3;
              return call(queryCurrent);

            case 3:
              user = _context2.sent;

              if (!(user && !(user instanceof Error))) {
                _context2.next = 7;
                break;
              }

              _context2.next = 7;
              return put({
                type: 'saveCurrentUser',
                payload: _objectSpread({}, user)
              });

            case 7:
              return _context2.abrupt("return", user);

            case 8:
            case "end":
              return _context2.stop();
          }
        }
      }, fetchCurrent);
    }),
    fetchTenantList: /*#__PURE__*/_regeneratorRuntime.mark(function fetchTenantList(_, _ref3) {
      var call, put, tenant;
      return _regeneratorRuntime.wrap(function fetchTenantList$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              call = _ref3.call, put = _ref3.put;
              _context3.next = 3;
              return call(queryTenant);

            case 3:
              tenant = _context3.sent;
              _context3.next = 6;
              return put({
                type: 'saveTenant',
                payload: tenant
              });

            case 6:
              return _context3.abrupt("return", tenant);

            case 7:
            case "end":
              return _context3.stop();
          }
        }
      }, fetchTenantList);
    }),
    fetchRoleList: /*#__PURE__*/_regeneratorRuntime.mark(function fetchRoleList(_ref4, _ref5) {
      var organizationId, call, put, res, response;
      return _regeneratorRuntime.wrap(function fetchRoleList$(_context4) {
        while (1) {
          switch (_context4.prev = _context4.next) {
            case 0:
              organizationId = _ref4.payload.organizationId;
              call = _ref5.call, put = _ref5.put;
              _context4.next = 4;
              return call(queryRoleList, organizationId);

            case 4:
              res = _context4.sent;
              response = getResponse(res);

              if (!response) {
                _context4.next = 9;
                break;
              }

              _context4.next = 9;
              return put({
                type: 'saveRoleList',
                payload: response
              });

            case 9:
            case "end":
              return _context4.stop();
          }
        }
      }, fetchRoleList);
    }),
    updateCurrentRole: /*#__PURE__*/_regeneratorRuntime.mark(function updateCurrentRole(_ref6, _ref7) {
      var roleId, call, res;
      return _regeneratorRuntime.wrap(function updateCurrentRole$(_context5) {
        while (1) {
          switch (_context5.prev = _context5.next) {
            case 0:
              roleId = _ref6.payload.roleId;
              call = _ref7.call;
              _context5.next = 4;
              return call(_updateCurrentRole, roleId);

            case 4:
              res = _context5.sent;
              return _context5.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context5.stop();
          }
        }
      }, updateCurrentRole);
    }),
    updateCurrentTenant: /*#__PURE__*/_regeneratorRuntime.mark(function updateCurrentTenant(_ref8, _ref9) {
      var tenantId, call, res;
      return _regeneratorRuntime.wrap(function updateCurrentTenant$(_context6) {
        while (1) {
          switch (_context6.prev = _context6.next) {
            case 0:
              tenantId = _ref8.payload.tenantId;
              call = _ref9.call;
              _context6.next = 4;
              return call(_updateCurrentTenant, tenantId);

            case 4:
              res = _context6.sent;
              return _context6.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context6.stop();
          }
        }
      }, updateCurrentTenant);
    }),
    updateDefaultTenant: /*#__PURE__*/_regeneratorRuntime.mark(function updateDefaultTenant(_ref10, _ref11) {
      var tenantId, call, res;
      return _regeneratorRuntime.wrap(function updateDefaultTenant$(_context7) {
        while (1) {
          switch (_context7.prev = _context7.next) {
            case 0:
              tenantId = _ref10.payload.tenantId;
              call = _ref11.call;
              _context7.next = 4;
              return call(_updateDefaultTenant, tenantId);

            case 4:
              res = _context7.sent;
              return _context7.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context7.stop();
          }
        }
      }, updateDefaultTenant);
    }),
    fetchDataHierarchiesList: /*#__PURE__*/_regeneratorRuntime.mark(function fetchDataHierarchiesList(_ref12, _ref13) {
      var payload, call, put, hierarchicalList, response;
      return _regeneratorRuntime.wrap(function fetchDataHierarchiesList$(_context8) {
        while (1) {
          switch (_context8.prev = _context8.next) {
            case 0:
              payload = _ref12.payload;
              call = _ref13.call, put = _ref13.put;
              _context8.next = 4;
              return call(_fetchDataHierarchiesList, payload);

            case 4:
              hierarchicalList = _context8.sent;
              response = getResponse(hierarchicalList);
              _context8.next = 8;
              return put({
                type: 'updateState',
                payload: {
                  hierarchicalList: response
                }
              });

            case 8:
              return _context8.abrupt("return", response);

            case 9:
            case "end":
              return _context8.stop();
          }
        }
      }, fetchDataHierarchiesList);
    }),
    queryDataHierarchies: /*#__PURE__*/_regeneratorRuntime.mark(function queryDataHierarchies(_ref14, _ref15) {
      var payload, select, call, put, dataHierarchyFlag, hierarchicalList, response;
      return _regeneratorRuntime.wrap(function queryDataHierarchies$(_context9) {
        while (1) {
          switch (_context9.prev = _context9.next) {
            case 0:
              payload = _ref14.payload;
              select = _ref15.select, call = _ref15.call, put = _ref15.put;
              _context9.next = 4;
              return select(function (state) {
                return state.user.currentUser && state.user.currentUser.dataHierarchyFlag;
              });

            case 4:
              dataHierarchyFlag = _context9.sent;

              if (!dataHierarchyFlag) {
                _context9.next = 14;
                break;
              }

              _context9.next = 8;
              return call(_queryDataHierarchies, payload);

            case 8:
              hierarchicalList = _context9.sent;
              response = getResponse(hierarchicalList);

              if (!response) {
                _context9.next = 13;
                break;
              }

              _context9.next = 13;
              return put({
                type: 'updateState',
                payload: {
                  hierarchicalSelectList: response.selectList || [],
                  isModal: response.hasModal,
                  isSelect: response.hasSelect
                }
              });

            case 13:
              return _context9.abrupt("return", response);

            case 14:
            case "end":
              return _context9.stop();
          }
        }
      }, queryDataHierarchies);
    }),
    fetchDataHierarchiesSelectList: /*#__PURE__*/_regeneratorRuntime.mark(function fetchDataHierarchiesSelectList(_ref16, _ref17) {
      var payload, call, hierarchicalList, response;
      return _regeneratorRuntime.wrap(function fetchDataHierarchiesSelectList$(_context10) {
        while (1) {
          switch (_context10.prev = _context10.next) {
            case 0:
              payload = _ref16.payload;
              call = _ref17.call;
              _context10.next = 4;
              return call(_fetchDataHierarchiesSelectList, payload);

            case 4:
              hierarchicalList = _context10.sent;
              response = getResponse(hierarchicalList); // yield put({
              //   type: 'updateState',
              //   payload: { hierarchicalList: response },
              // });

              return _context10.abrupt("return", response);

            case 7:
            case "end":
              return _context10.stop();
          }
        }
      }, fetchDataHierarchiesSelectList);
    }),
    switchDataHierarchies: /*#__PURE__*/_regeneratorRuntime.mark(function switchDataHierarchies(_ref18, _ref19) {
      var payload, call, hierarchicalList, response;
      return _regeneratorRuntime.wrap(function switchDataHierarchies$(_context11) {
        while (1) {
          switch (_context11.prev = _context11.next) {
            case 0:
              payload = _ref18.payload;
              call = _ref19.call;
              _context11.next = 4;
              return call(_switchDataHierarchies, payload);

            case 4:
              hierarchicalList = _context11.sent;
              response = getResponse(hierarchicalList);
              return _context11.abrupt("return", response);

            case 7:
            case "end":
              return _context11.stop();
          }
        }
      }, switchDataHierarchies);
    })
  },
  reducers: {
    save: function save(state, action) {
      return _objectSpread(_objectSpread({}, state), {}, {
        list: action.payload
      });
    },
    saveCurrentUser: function saveCurrentUser(state, action) {
      return _objectSpread(_objectSpread({}, state), {}, {
        currentUser: action.payload
      });
    },
    updateCurrentUser: function updateCurrentUser(state, action) {
      return _objectSpread(_objectSpread({}, state), {}, {
        currentUser: _objectSpread(_objectSpread({}, state.currentUser), action.payload)
      });
    },
    changeNotifyCount: function changeNotifyCount(state, action) {
      return _objectSpread(_objectSpread({}, state), {}, {
        currentUser: _objectSpread(_objectSpread({}, state.currentUser), {}, {
          notifyCount: action.payload
        })
      });
    },
    saveTenant: function saveTenant(state, action) {
      return _objectSpread(_objectSpread({}, state), {}, {
        tenantList: action.payload
      });
    },
    // saveDefaultRole(state, action) {
    //   return {
    //     ...state,
    //     currentUser: action.payload,
    //   };
    // },
    saveRoleList: function saveRoleList(state, action) {
      return _objectSpread(_objectSpread({}, state), {}, {
        roleList: action.payload
      });
    },
    updateState: function updateState(state, action) {
      return _objectSpread(_objectSpread({}, state), action.payload);
    }
  }
};